//
//  ByApiUser.m
//  caipiao_ios
//
//  Created by mc on 2018/7/10.
//  Copyright © 2018年 ZhangZhanbin. All rights reserved.
//

#import "ByApiUser.h"

@implementation ByApiUser

/** 登陆 */
+ (void)wechatLogin:(ByNetParamsModel *)model
            success:(void(^)(ByNetBaseResult *result))success
            failure:(void(^)(ByNetBaseResult *result))failure{
    
    

    // 拼接参数
    NSMutableDictionary *params = [[NSMutableDictionary alloc] init];
    [params setObject:[ByBallApi toJSONData:model.dic] forKey:@"user_info"];
    
    if (model.mobile) {
        [params setObject:model.mobile forKey:@"mobile"];
    }
    
    if (model.mobile_code) {
        [params setObject:model.mobile_code forKey:@"mobile_code"];
    }
    
    if (model.invite_code) {
        [params setObject:model.invite_code forKey:@"invite_code"];
    }
    
    NSLog(@"params = %@", params);
  
    [self post:@"/weixin/app_login" params:params success:^(id result) {
        
        ByNetBaseResult *result1 = [[ByNetBaseResult alloc] init];
        result1.code = 0;
        result1.msg = @"登录成功";
        result1.data = result;
        if(success) {
            success(result1);
        }
    } failure:^(NSError *error) {
        
        ByNetBaseResult *result = [[ByNetBaseResult alloc] init];
        result.code = error.code;
        result.msg = error.domain;
        result.data = error.localizedDescription;
        if(failure) {
            failure(result);
        } else {
            
        }
       
    }];
}

//用户普通信息更新
+ (void)userInfoUpdate:(ByNetParamsModel *)model
               success:(void(^)(ByNetBaseResult *result))success
               failure:(void(^)(ByNetBaseResult *result))failure{
    
    
    
    // 拼接参数
    NSMutableDictionary *params = [[NSMutableDictionary alloc] init];
    [params setObject:model.nickname forKey:@"nick"];
   
    
    [self post:@"/user_info/update" params:params success:^(id result) {
        
        ByNetBaseResult *result1 = [[ByNetBaseResult alloc] init];
        result1.code = 0;
        result1.msg = @"更新成功";
        result1.data = result;
        if(success) {
            success(result1);
        }
    } failure:^(NSError *error) {
        
        ByNetBaseResult *result = [[ByNetBaseResult alloc] init];
        result.code = error.code;
        result.msg = error.domain;
        result.data = error.localizedDescription;
        if(failure) {
            failure(result);
        } else {
            
        }
        
    }];
    
    
}

//头像上传
+ (void)userFileUploadAvatar:(UIImage *)image
                     success:(void(^)(ByNetBaseResult *result))success
                     failure:(void(^)(ByNetBaseResult *result))failure{
   
    // 处理图片
    NSData *data = UIImageJPEGRepresentation(image, 1);
    while ((data.length / 1000.0) > 1000) {
        UIImage *tmp_image = [UIImage imageWithData:data];
        data = UIImageJPEGRepresentation(tmp_image, 0.5);
    }
    
    NSLog(@"图片上传长度 = %ld", data.length);
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    // 默认超时时间30秒
    manager.requestSerializer.timeoutInterval = 10;
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"text/html",@"application/json", @"text/json", @"text/javascript", nil];
    
    [manager.requestSerializer setValue:@"SocketLog(tabid=2&client_id=slog_fc68e9)" forHTTPHeaderField:@"SocketLog"];
    
    // 数据加密
    NSMutableDictionary *m_params = [[NSMutableDictionary alloc] init];
    [m_params setObject:@"prod" forKey:@"env"];
    if (BY_USER && BY_USER.token) {
        [m_params setObject:BY_USER.token forKey:@"token"];
    }
    [m_params setObject:data forKey:@"avatar"];
    [m_params setObject:APP_VERSION forKey:@"app_version"];
    [m_params setObject:@"ios" forKey:@"app_type"];
    
    NSString *str = [NSString stringWithFormat:@"%@/file/upload_avatar",BOYE_BASE_URL];
    
     //拼接请求体
    NSMutableURLRequest *request = [[AFHTTPRequestSerializer serializer] multipartFormRequestWithMethod:@"POST" URLString:str parameters:m_params constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
            [formData appendPartWithFileData:data name:@"image" fileName:@"icon.jpg" mimeType:@"image/jpeg"];
    
    } error:nil];
    
    
        NSURLSessionUploadTask *uploadTask;
        uploadTask = [manager
                      uploadTaskWithStreamedRequest:request
                      progress:^(NSProgress * _Nonnull uploadProgress) {
    
                          NSLog(@"fractionCompleted: %f", uploadProgress.fractionCompleted);
    
                          // 根据请求进度刷新UI
                          dispatch_async(dispatch_get_main_queue(), ^{
                          });
    
                      }
                      completionHandler:^(NSURLResponse * _Nonnull response, id  _Nullable responseObject, NSError * _Nullable error) {
                         
                                        NSDictionary *dic = responseObject;
                                        NSString *code = dic[@"code"];
                          
                                        NSLog(@"头像上传 str = %@,responseObject = %@", str,responseObject);
                                        ByNetBaseResult *result1 = [[ByNetBaseResult alloc] init];
                                        result1.code = 0;
                                        result1.msg = @"更新成功";
                                        result1.data = responseObject;
                          
                                        if (code.integerValue != 0) {
                          
                                            result1.msg = dic[@"msg"];
                                            if(failure) {
                                                failure(result1);
                                            }
                          
                                        }else{
                          
                                        if(success) {
                                            success(result1);
                                        }
                          
                                      }
                      }];
    
        [uploadTask resume];
}


//短信验证码发送接口
+ (void)smsSendCode:(ByNetParamsModel *)model
            success:(void(^)(ByNetBaseResult *result))success
            failure:(void(^)(ByNetBaseResult *result))failure{
    
    
    // 拼接参数
    NSMutableDictionary *params = [[NSMutableDictionary alloc] init];
    [params setObject:model.mobile forKey:@"mobile"];
    [params setObject:model.type forKey:@"type"];
    
    [self post:@"/sms/send_code" params:params success:^(id result) {
        
        ByNetBaseResult *result1 = [[ByNetBaseResult alloc] init];
        result1.code = 0;
        result1.msg = @"验证码发送成功";
        result1.data = result;
        if(success) {
            success(result1);
        }
    } failure:^(NSError *error) {
        
        ByNetBaseResult *result = [[ByNetBaseResult alloc] init];
        result.code = error.code;
        result.msg = error.domain;
        result.data = error.localizedDescription;
        if(failure) {
            failure(result);
        } else {
            
        }
        
    }];
    
}

//用户手机号绑定
+ (void)userBindPhone:(ByNetParamsModel *)model
              success:(void(^)(ByNetBaseResult *result))success
              failure:(void(^)(ByNetBaseResult *result))failure{
    
    // 拼接参数
    NSMutableDictionary *params = [[NSMutableDictionary alloc] init];
    [params setObject:model.mobile forKey:@"mobile"];
    [params setObject:model.code forKey:@"code"];
    [self post:@"/user_info/bind_phone" params:params success:^(id result) {
        
        ByNetBaseResult *result1 = [[ByNetBaseResult alloc] init];
        result1.code = 0;
        result1.msg = @"验证码发送成功";
        result1.data = result;
        if(success) {
            success(result1);
        }
    } failure:^(NSError *error) {
        
    
        ByNetBaseResult *result = [[ByNetBaseResult alloc] init];
        result.code = error.code;
        result.msg = error.domain;
        result.data = error.localizedDescription;
        if(failure) {
            failure(result);
        } else {
            
        }
        
    }];
    
}


//用户更换手机号
+ (void)userChangeMobile:(ByNetParamsModel *)model
                 success:(void(^)(ByNetBaseResult *result))success
                 failure:(void(^)(ByNetBaseResult *result))failure{
    
    // 拼接参数
    NSMutableDictionary *params = [[NSMutableDictionary alloc] init];
    [params setObject:model.mobile forKey:@"new_mobile"];
    [params setObject:model.code forKey:@"new_mobile_code"];
    
    [self post:@"/user_info/change_phone" params:params success:^(id result) {
        
        ByNetBaseResult *result1 = [[ByNetBaseResult alloc] init];
        result1.code = 0;
        result1.msg = @"验证码发送成功";
        result1.data = result;
        if(success) {
            success(result1);
        }
    } failure:^(NSError *error) {
        
        
        ByNetBaseResult *result = [[ByNetBaseResult alloc] init];
        result.code = error.code;
        result.msg = error.domain;
        result.data = error.localizedDescription;
        if(failure) {
            failure(result);
        } else {
            
        }
        
    }];
    
}

//是否注册过微信
+ (void)wechatIsRegister:(ByNetParamsModel *)model
                 success:(void(^)(ByNetBaseResult *result))success
                 failure:(void(^)(ByNetBaseResult *result))failure{

    // 拼接参数
    NSMutableDictionary *params = [[NSMutableDictionary alloc] init];
    [params setObject:model.code forKey:@"code"];
    [self post:@"/weixin/is_register" params:params success:^(id result) {
        
        ByNetBaseResult *result1 = [[ByNetBaseResult alloc] init];
        result1.code = 0;
        result1.msg = @"登录成功";
        result1.data = result;
        if(success) {
            success(result1);
        }
        
    } failure:^(NSError *error) {
        
        ByNetBaseResult *result = [[ByNetBaseResult alloc] init];
        result.code = error.code;
        result.msg = error.domain;
        result.data = error.localizedDescription;
        if(failure) {
            failure(result);
        } else {
            
        }
        
    }];
}


//登录接口
+ (void)userAccountLogin:(ByNetParamsModel *)model
                 success:(void(^)(ByNetBaseResult *result))success
                 failure:(void(^)(ByNetBaseResult *result))failure{
    
    
    // 拼接参数
    NSMutableDictionary *params = [[NSMutableDictionary alloc] init];
    if (model.mobile) {
        [params setObject:model.mobile forKey:@"mobile"];
    }
    
    if (model.mobile_code) {
        [params setObject:model.mobile_code forKey:@"mobile_code"];
    }
    
    
    [self post:@"/user_account/login" params:params success:^(id result) {
        
        NSLog(@"新登录 = %@", result);
        
        ByNetBaseResult *result1 = [[ByNetBaseResult alloc] init];
        result1.code = 0;
        result1.msg = @"登录成功";
        result1.data = result;
        if(success) {
            success(result1);
        }
    } failure:^(NSError *error) {
        
        ByNetBaseResult *result = [[ByNetBaseResult alloc] init];
        result.code = error.code;
        result.msg = error.domain;
        result.data = error.localizedDescription;
        if(failure) {
            failure(result);
        } else {
            
        }
        
    }];
    
    
}


//注册接口
+ (void)userAccountRegister:(ByNetParamsModel *)model
                    success:(void(^)(ByNetBaseResult *result))success
                    failure:(void(^)(ByNetBaseResult *result))failure{
    
    
    // 拼接参数
    NSMutableDictionary *params = [[NSMutableDictionary alloc] init];
    if (model.mobile) {
        [params setObject:model.mobile forKey:@"mobile"];
    }
    
    if (model.mobile_code) {
        [params setObject:model.mobile_code forKey:@"mobile_code"];
    }
    
    if (model.invite_code) {
        [params setObject:model.invite_code forKey:@"invite_code"];
    }
    
    [self post:@"/user_account/register" params:params success:^(id result) {
        
        ByNetBaseResult *result1 = [[ByNetBaseResult alloc] init];
        result1.code = 0;
        result1.msg = @"登录成功";
        result1.data = result;
        if(success) {
            success(result1);
        }
        
    } failure:^(NSError *error) {
        
        ByNetBaseResult *result = [[ByNetBaseResult alloc] init];
        result.code = error.code;
        result.msg = error.domain;
        result.data = error.localizedDescription;
        if(failure) {
            failure(result);
        } else {
            
        }
        
    }];
    
}


@end
